package com.example.lotterysystem.service.activitystatus.operater;

import com.example.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.example.lotterysystem.common.exception.ServiceException;
import com.example.lotterysystem.dao.dataobject.ActivityDo;
import com.example.lotterysystem.dao.mapper.ActivityMapper;
import com.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.example.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import com.example.lotterysystem.service.enums.ActivityStatusEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivityOperator extends AbstractActivityOperator {

    private static final Logger logger = LoggerFactory.getLogger(ActivityOperator.class);

    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 2;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();

        ActivityStatusEnum targetStatus = convertActivityStatusDTO.getTargetActivityStatus();
        if(activityId == null || targetStatus == null){
            return false;
        }
        ActivityDo activityDo = activityMapper.selectById(activityId);
        if(activityDo == null){
            return false;
        }

        if(targetStatus.name().equalsIgnoreCase(activityDo.getStatus())){
            return false;
        }
        int count = activityPrizeMapper.countPrize(activityId, ActivityPrizeStatusEnum.INIT.name());
        if(count > 0){
            return false;
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {

        try {
            Long activityId = convertActivityStatusDTO.getActivityId();
            ActivityStatusEnum targetStatus = convertActivityStatusDTO.getTargetActivityStatus();
            activityMapper.updateStatus(activityId , targetStatus.name());

            return true;
        }catch (Exception e){
            return false;
        }


    }
}








